jQuery(document).ready(function() {
	
	resizeIframe();
	
    jQuery(window).on("resize", function(event){
       resizeIframe();
    }); 
	
	var $select2 = jQuery('#gadgetdetails select').select2();

	$select2.on('select2-open', function(e) {
		jQuery('.select2-drop-active .select2-input').blur();
	});
	
    var current_pos = getCookie("tiangge_current_pos");

      if (current_pos == "") {
          setCookie("tiangge_current_pos","0");
      }
	  
	  if (current_pos > 0){
	      //alert(current_pos + "TRUE");
	      if(current_pos=="1") {
	    	 
	          jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
	          jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
	          jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	       
	          var mode = getCookie("tiangge_mode");
	          var imei = getCookie("tiangge_imei");
	          var model = getCookie("tiangge_model");
	          var price = getCookie("tiangge_price");
	          var ORNumber = getCookie("tiangge_ORNumber");
	          
	          if(mode!="edit"){    
	        	     jQuery(".deviceBox").addClass("active");
	                 jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox").removeClass("active");
	                 jQuery('#pname, #pemail, #pdob, #padd').hide();
	                 jQuery('#promo, #commitmentBox, #mobtel').text("None");
	                 jQuery('#gadget').text(model + " - " + price);
	                 jQuery('#imei').val(imei);
	                 jQuery('#model').val(model);
					 jQuery('#price').val(price.replace("P", ""));
					 jQuery('#ORNumber').val(ORNumber);
	          }
	    }
	    if(current_pos=="2") {
	    	
	          jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
	          jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
	          jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	          
	          var mode = getCookie("tiangge_mode");
	          var imei = getCookie("tiangge_imei");
	          var model = getCookie("tiangge_model");
	          var price = getCookie("tiangge_price");
	          var ORNumber = getCookie("tiangge_ORNumber");
	          var promo = getCookieR("tiangge_promo");
			  var promoDesc = getCookieR("tiangge_promoDesc");
	          var commitmentBox = getCookieR("tiangge_commitmentBox");
	      
	          if(mode!="edit"){  
	        	  jQuery(".deviceBox, .promoBox, .commitmentBox").addClass("active");
	              jQuery(".prepaidBox, .profileBox").removeClass("active");
	              jQuery('#pname, #pemail, #pdob, #padd').hide();
	              jQuery('#mobtel').text("None");
	              jQuery('#gadget').text(model + " - " + price);
	              jQuery('#imei').val(imei);
	              jQuery('#model').val(model);
	              // select_value = jQuery("#price").select2("val");
				  // jQuery("select#price").select2('val', select_value);
				  jQuery('#price').val(price.replace("P", ""));
	              jQuery('#ORNumber').val(ORNumber);
	              if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
					promoDesc = promoDesc.replace("john", ",");
					promoDesc = promoDesc.replace("john ", ", ");
					jQuery('#promo').html(promo + promoDesc);
				  } else {
					jQuery('#promo').html(promo + promoDesc);
				  }
				  jQuery('#commitmentBox').text(commitmentBox);
	          }
	         
	      }
	      if(current_pos=="3" || current_pos=="4") {
	          jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
	          jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
	          jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");

	          var mode = getCookie("tiangge_mode");
	          var imei = getCookie("tiangge_imei");
	          var model = getCookie("tiangge_model");
	          var price = getCookie("tiangge_price");
	          var ORNumber = getCookie("tiangge_ORNumber");
			  var promoDesc = getCookieR("tiangge_promoDesc");
	          var commitmentBox = getCookieR("tiangge_commitmentBox");
	          var mobtel = getCookieR("tiangge_mobtel");
			 
	          if(mode!="edit"){      
		      	jQuery(".deviceBox, .promoBox, .commitmentBox, .prepaidBox").addClass("active");
	            jQuery('#pname, #pemail, #pdob, #padd').hide();
	            jQuery('#gadget').text(model + " - " + price);
	            jQuery('#imei').val(imei);
	            jQuery('#model').val(model);
				jQuery('#mobtel').text(mobtel);
	            //jQuery("#price").select2('val', price);
				//jQuery("#s2id_price .select2-chosen").text(price);
				jQuery('#price').val(price.replace("P", ""));
	            jQuery('#ORNumber').val(ORNumber);
				if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
					promoDesc = promoDesc.replace("john", ",");
					promoDesc = promoDesc.replace("john ", ", ");
					jQuery('#promo').html(promo + promoDesc);
				} else {
					jQuery('#promo').html(promo + promoDesc);
				}
				jQuery('#commitmentBox').text(commitmentBox);
	          }
	      }
	  } else {
	    //alert(current_pos + "FALSE");
	    jQuery("div.wizard a:nth-of-type(1)").attr("class","current");
	    jQuery("div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
	  
	  }

	  //Start of Cut Copy Paste Disable 
	  jQuery("#imei, #model, #ORNumber").bind("cut copy paste",function(e) {
	     e.preventDefault();
	  });
	  
	  //Number only
	  jQuery("#imei").keypress(function (evt) {
		 evt = evt || window.event;
	  	 var charCode = evt.which || evt.keyCode;
	  	 var charStr = String.fromCharCode(charCode);
		 if ( /[a-z0-9]/i.test(charStr) || evt.which == 8 || evt.which == 9 || (evt.which > 36 || evt.which < 41)) {
	  	 }else{
		  return false;
		 }
		 var imei = jQuery('#imei');
		 if (evt.which != 8 && evt.which != 0 && (evt.which < 48 || evt.which > 57)) {
	      return false;
	     }
		if (jQuery.trim(imei.val()).length > 15) {
			//alert("16");
			if (evt.which == 8 || evt.which == 46){
				return true;
			}
			return false;
		}
		
		jQuery('#imei').focus();
	  }); 
	  
	  jQuery('#imei').keyup(function (evt) {
		 if (this.value != this.value.replace(/[^0-9]/g, '')) {
		  this.value = this.value.replace(/[^0-9]/g, '');
		 }
		 
		var imei = jQuery('#imei');
		var imeiErr = jQuery('#imeiError');
        if (jQuery.trim(imei.val()).length > 15) {
            imei.addClass("error");
			imeiErr.show();
        } else {
            imei.removeClass("error");
			imeiErr.hide();
        }
		jQuery('#imei').focus();
	  });


	  jQuery('#model').keyup(function(){
		var model = jQuery('#model');
		var modelErr = jQuery('#modelError');
        if (jQuery.trim(model.val()).length > 25) {
            model.addClass("error");
			modelErr.show();
        } else {
            model.removeClass("error");
			modelErr.hide();
        }		   
	  });		  
	  
	  jQuery("#price").keypress(function (evt) {
		 //if the letter is not digit, not comma, not period then display error and don't type anything	 
	  	var charCode = evt.which || evt.keyCode;		
		if (charCode != 44 && charCode != 46 && evt.which != 0 && evt.which != 8  && (evt.which < 48 || evt.which > 57)) {
			return false;
		} else {
			var prc = jQuery("#price").val();
			//console.log(jQuery("#price").val());
			//console.log(jQuery.isNumeric(prc));
			//console.log("amount-"+parseInt(prc.replace(",","")));
			if(jQuery.isNumeric(parseInt(prc.replace(",","")))==true && prc != 0){
				jQuery('#price').removeClass("error");
			    jQuery('#price-inner .errorBox').hide();
			}else{
				jQuery('#price').addClass("error");
				jQuery('#price-inner .errorBox').show();
			}
	    }
	  }); 
	  
	  jQuery("#price").keyup(function (evt) {
		 //if the letter is not digit, not comma, not period then display error and don't type anything	 
	  	var charCode = evt.which || evt.keyCode;		
		if (charCode != 44 && charCode != 46 && evt.which != 0 && evt.which != 8  && (evt.which < 48 || evt.which > 57)) {
			return false;
		} else {
			var prc = jQuery("#price").val();
			//console.log(jQuery("#price").val());
			//console.log(jQuery.isNumeric(prc));
			//console.log("amount-"+parseInt(prc.replace(",","")));
			if(jQuery.isNumeric(parseInt(prc.replace(",","")))==true && prc != 0){
				jQuery('#price').removeClass("error");
			    jQuery('#price-inner .errorBox').hide();
			}else{
				jQuery('#price').addClass("error");
				jQuery('#price-inner .errorBox').show();
			}
	    }
	  }); 
	  
	  //Special Characters not allowed
	  jQuery('#ORNumber').keyup(function () {
		 if (this.value != this.value.replace(/[^\w\d]/g, '')) {
		  this.value = this.value.replace(/[^\w\d]/g, '');
		 }
		 if (this.value != this.value.replace(/_/, '')) {
		  this.value = this.value.replace(/_/, '');
		 }
		 
		var ornumber = jQuery('#ORNumber');
		var ornumberErr = jQuery('#ornumberError');
        if (jQuery.trim(ornumber.val()).length < 3 ) {
            ornumber.addClass("error");
			ornumberErr.show();
        } else {
            ornumber.removeClass("error");
			ornumberErr.hide();
        }
	  });
	  
	 	  
	    if(/gadgetdetails/.test(self.location.href)){
            var mode = getCookie("tiangge_mode");
            var gadget = getCookieR("tiangge_model") + " - " + getCookieR("tiangge_price");
            var promo = getCookieR("tiangge_promo");
            var promoDesc = getCookieR("tiangge_promoDesc");
            var commitmentBox = getCookieR("tiangge_commitmentBox");
            var mobtel = getCookieR("tiangge_mobtel");
            var username = getCookieR("tiangge_username");
            var eadd = getCookieR("tiangge_eadd");
            var bday = getCookieR("tiangge_bday");
            var address = getCookieR("tiangge_address");
            var area = getCookieR("tiangge_area");
            var town = getCookieR("tiangge_town");
            var province = getCookieR("tiangge_province");
            var postalcode = getCookieR("tiangge_postalcode");
            var imei = getCookie("tiangge_imei");
            var model = getCookie("tiangge_model");
            var price = getCookie("tiangge_price");
            var ORNumber = getCookie("tiangge_ORNumber");

            if(mode=="edit"){
                jQuery(".promoBox").addClass("active");
                if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
                   promoDesc = promoDesc.replace("john", ",");
                   promoDesc = promoDesc.replace("john ", ", ");
                   jQuery('#promo').html(promo + promoDesc);
                } else {
                   jQuery('#promo').html(promo + promoDesc);
                }
                jQuery(".deviceBox").addClass("active");
                jQuery('#gadget').text(gadget);
                jQuery(".commitmentBox").addClass("active");
                jQuery('#commitmentBox').text(commitmentBox);
                jQuery(".prepaidBox").addClass("active");
                if(mobtel=="0"){jQuery('#mobtel').text("None");} else {jQuery('#mobtel').text(mobtel);}
                jQuery(".pnone").hide();
                jQuery(".profileBox").addClass("active");
                jQuery('#name').text(username);
                jQuery('#email').text(eadd);
                jQuery('#dob').text(bday);

                if(province=="Metro Manila") 
                   var address = address + ', ' + area + ', ' + town + ', ' + province + ', ' + postalcode;
                else
                   var address = address + ', ' + town + ', ' + province + ', ' + postalcode;

                jQuery('#add').text(address);
                jQuery('#imei').val(imei);
                jQuery('#model').val(model);
                jQuery('#price').val(price.replace("P", ""));
                jQuery('#ORNumber').val(ORNumber);
                jQuery("#pname").show();
                jQuery("#pemail").show();
                jQuery("#pdob").show();
                jQuery("#padd").show();
                jQuery('#btn_next2').text("Save");
	        }
	     }
	  

	
	
});


(function (w ) {
   
    w.fn.wGadgetDetails = function(options) {
        // These are the defaults.
        var s = w.extend({
        }, options );
        
        w("#gadgetdetails select").select2({minimumResultsForSearch: -1});
        
        var methods = {
            alphenum:function(evt){
                evt = evt || window.event;
                var charCode = evt.which || evt.keyCode;
                var charStr = String.fromCharCode(charCode);
                if ( /[a-z0-9]/i.test(charStr) || evt.which == 8 || evt.which == 9 || (evt.which > 36 || evt.which < 41)) {
                }else{
                    return false;
                }
                if (evt.which != 8 && evt.which != 0 && (evt.which < 48 || evt.which > 57)&&
                    (evt.which < 65 || evt.which > 89) && (evt.which < 97 || evt.which > 122)) {
                    return false;
                }
                
            },
            validateEmpty: function(elem){
                if (w.trim(elem.val()).length === 0) {
                    elem.addClass("error");
                    return false;
                } else {
                    elem.removeClass("error");
                    return true;
                }
            },
            validateLen: function(elem, min){
                if (w.trim(elem.val()).length < min) {
                    elem.addClass("error");	
                    return false;
                } else {
                    elem.removeClass("error");
                    return true;
                }
            },
            validateMaxLen: function(elem, min, max){
				//alert("length"+w.trim(elem.val()).length+"min-"+min+"max-"+max);
                if (w.trim(elem.val()).length < min) {
                    elem.addClass("error");	
                    return false;
                } else if (w.trim(elem.val()).length > max) {
                    elem.addClass("error");	
                    return false;
                } else {
                    elem.removeClass("error");
                    return true;
                }
            },
			format2: function(n, currency){   
				return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
			}
        }; // methods
        
        
        w("#ORNumber").keypress(function (evt) {
            return methods.alphenum(evt);
        }); 
        
//        w('#model').keypress(function (evt) {
//            //Comment out since accept anything. Fact Sheet
//            return methods.alphenum(evt);
//        }); 
        
      /**
       * Gadget Details next
       */
	  w("#btn_next").click(function() { 
            var imei = w('#imei');
            //methods.validateLen(imei, 15);
			methods.validateMaxLen(imei, 15, 15);
			
            var model = w('#model');
            //methods.validateLen(model, 2);
			methods.validateMaxLen(model, 2, 25);
			
            var price = w('#price');
            methods.validateLen(price, 1);
			
			if(price.hasClass('error') || price.val()==0){
				price.addClass("error");
				price.focus();
				w('#price-inner .errorBox').show();
			} else {
				w('#price-inner .errorBox').hide();
			}

            var ORNumber = w('#ORNumber');
            methods.validateLen(ORNumber, 3);
			
			
            var hasErr =true;
						
			if(ORNumber.hasClass('error')){
                ORNumber.focus();
				w('#ornumber-inner .errorBox').show();
            } else {
				w('#ornumber-inner .errorBox').hide();
			}
			

			if(model.hasClass('error')){
                model.focus();
				w('#handset-inner .errorBox').show();
            } else {
				w('#handset-inner .errorBox').hide();
			}
			if(imei.hasClass('error')){
                imei.focus();
				w('#IMEI-inner .errorBox').show();
            } else {
				w('#IMEI-inner .errorBox').hide();
			}

			if(w("#imei, #model, #price, #ORNumber").hasClass('error')) {	
				scrollFrameHeight('300');
			} else {
                hasErr=false;
            }
			
			var current_pos = getCookie("tiangge_current_pos");
			
            if(!hasErr){
                if (current_pos < 1) {
                    setCookie("tiangge_current_pos","1");
                }
                console.log("gadgetdetails Success");		
                setCookie("tiangge_imei",imei.val());
                setCookie("tiangge_model",model.val());
				var price = parseInt(price.val().replace(",",""));
                setCookie("tiangge_price",methods.format2(price, "P"));
				
				if(price <= 2000) 
					var gadgetplanprice = 2000.00;
				else if(price >= 2001 && price <=3000)
					var gadgetplanprice = 3000.00;
				else if(price >= 3001 && price <=4000)
					var gadgetplanprice = 4000.00;					
				else if(price >= 4001 && price <=5000)
					var gadgetplanprice = 5000.00;	
				else
					var gadgetplanprice = 9200.00;
					
				setCookie("tiangge_planprice",gadgetplanprice);
                setCookie("tiangge_ORNumber",ORNumber.val()); 
                //setCookie("tiangge_gadgetdetails",);
                w("#gadgetdetails").submit();				
            }else{
                console.log("gadgetdetails Error");
            }
			resizeIframe();
        }); 
         
        /**
         * Disable right click
         */
        w(document).bind("contextmenu",function(e){
           //return false;
           e.preventDefault();
        });
    
        return methods;
    };
    
    w(document).ready(function(){
        var gadgetdetails = w(document).wGadgetDetails();
    });
    
}(jQuery));